package au.com.handsunited.web.controller.entity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.Errors;

import au.com.handsunited.model.BaseEntity;
import au.com.handsunited.model.User;
import au.com.handsunited.web.command.entity.AbstractInviteToCommand;
import au.com.handsunited.web.session.SessionSupport;

/**
 * @author arunda
 *
 */
public class AbstractInviteToController extends AbstractInviteController {


	private static Logger logger = LoggerFactory.getLogger(AbstractInviteToController.class);


	/**
	 *
	 */
	public AbstractInviteToController() {
		super();
	}


	/**
	 * @see org.springframework.web.servlet.mvc.SimpleFormController#referenceData(javax.servlet.http.HttpServletRequest, java.lang.Object, org.springframework.validation.Errors)
	 */
	@Override
	protected Map referenceData(HttpServletRequest request, Object command, Errors errors) throws Exception {

		Map<String, Object> data = new HashMap<String, Object>();
		data.put("users", SessionSupport.getUser(request).getUsersInNetwork());
		return data;
	}

	/**
	 *
	 * @param request
	 * @param command
	 * @throws Exception
	 */
	protected void formBackingObject(HttpServletRequest request, AbstractInviteToCommand command) throws Exception {

		command.setUserMap(SessionSupport.getUser(request).getMapOfUsersInNetwork());
		command.setEntity(SessionSupport.getCurrentlyActiveEntity(request));
	}

	/**
	 *
	 * @param usersMap
	 * @param invitedUserIds
	 * @return
	 */
	protected List<BaseEntity> getInvitedEntities (Map<Long, User> usersMap, List<String> invitedUserIds) {

		List<BaseEntity> invitedEntities = new ArrayList<BaseEntity>();

		for (String userId : invitedUserIds) {

			User invitedUser = usersMap.get(Long.parseLong(userId));
			invitedEntities.add(invitedUser);
		}

		return invitedEntities;
	}

}

